package com.copote.pdfutils.common;

/**
 * @ClassName: PageHeader
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: 湖南湘邮科技股份有限公司/RedStar
 * @date: 2023/9/18 16:24
 * @Copyright: 2019 2000 - 2022 HuNan Copote Science & Technology CO., LTD. All Rights Reserved.<br/>
 * 注意：本内容仅限于湖南湘邮科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * PDF页眉
 *
 * @author: mcnlshen
 * @date: 2021-09-18 00:36
 */
@Slf4j
@AllArgsConstructor
public class PageHeader implements IEventHandler {
    private String headerPic;
    private String headerStr;
    private PdfFont pdfFont;

    @SneakyThrows
    @Override
    public void handleEvent(Event event) {
        final PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        final PdfDocument pdfDoc = docEvent.getDocument();
        final Document doc = new Document(pdfDoc);
        final PdfPage page = docEvent.getPage();
        final Rectangle pageSize = page.getPageSize();
        final float pdfWidth = pageSize.getWidth();
        final float pdfHeight = pageSize.getHeight();
        final float tableWidth = pdfWidth - doc.getRightMargin() - doc.getLeftMargin();
        // 页眉
        final Table headerTable = new Table(2);
        headerTable.setFixedLayout();
        headerTable.setWidth(tableWidth);
        headerTable.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //log.info(this.getClass().getResource(headerPic).toString());
        // 设置图片
        Cell imgCell = new Cell();
        if (headerStr.isEmpty()) {
        }else{
            Image img = new Image(ImageDataFactory.create(headerPic));//this.getClass().getResource(headerPic)
            img.setHeight(30);
            Paragraph imgParagraph = new Paragraph().add(img);
            imgParagraph.setMarginLeft(-20);
            imgCell.setBorder(Border.NO_BORDER);
            imgCell.setHorizontalAlignment(HorizontalAlignment.LEFT);
            imgCell.setVerticalAlignment(VerticalAlignment.BOTTOM);
            imgCell.setTextAlignment(TextAlignment.LEFT);
            imgCell.add(imgParagraph);
        }
        headerTable.addCell(imgCell);

        final Paragraph righText = new Paragraph();
        righText.setVerticalAlignment(VerticalAlignment.BOTTOM);
//        righText.add(new Tab()).addTabStops(new TabStop(800, TabAlignment.RIGHT));
        //righText.setBorder(new SolidBorder(1));
        righText.add(headerStr).setFontSize(11).setFont(pdfFont);
        righText.setMarginRight(19);
        righText.setPadding(1);
        final Cell rightCell = new Cell();
        rightCell.add(righText);
        rightCell.setWidth(160);
        rightCell.setFontSize(9f);
        rightCell.setBorder(Border.NO_BORDER);
        rightCell.setTextAlignment(TextAlignment.CENTER);
        //rightCell.setFontColor(lineColor);
        rightCell.setVerticalAlignment(VerticalAlignment.BOTTOM);
        rightCell.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        headerTable.addCell(rightCell);

        headerTable.setFixedPosition(doc.getLeftMargin(), pdfHeight - 45, tableWidth);
        doc.add(headerTable);
    }


}